package com.san.base.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.domain.LogDO;
import com.san.base.domain.PageDO;
import com.san.base.service.LogService;
import com.san.common.utils.Query;
import com.san.common.utils.R;

/**
 * 系统日志
 */
@RequestMapping("/base/log")
@Controller
public class LogController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(LogController.class);
	
	/**
	 * 本功能地址前缀
	 */
	private static final String PREFIX = "base/log";
	
	@Autowired
	LogService logService;

	@GetMapping()
	String log() {
		return PREFIX + "/log";
	}

	@ResponseBody
	@GetMapping("/list")
	PageDO<LogDO> list(@RequestParam Map<String, Object> params) {
		Query query = new Query(params);
		PageDO<LogDO> page = logService.queryList(query);
		return page;
	}

	@ResponseBody
	@PostMapping("/remove")
	R remove(Long id) {
		try {
			if (logService.remove(id) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("日志删除失败",e);
		}
		return R.error();
	}

	@ResponseBody
	@PostMapping("/batchRemove")
	R batchRemove(@RequestParam("ids[]") Long[] ids) {
		try {
			int r = logService.batchRemove(ids);
			if (r > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("日志批量删除失败",e);
		}
		
		return R.error();
	}

//	@RequiresPermissions("sys:log:show")
	@GetMapping("/show/{id}")
	String show(Model model, @PathVariable("id") Long id) {
		try {
			LogDO logDO = (LogDO)logService.getById(id);
			model.addAttribute("logDO", logDO);
		} catch (Exception e) {
			logger.error("获取日志信息失败",e);
		}
		
		return PREFIX + "/show";
	}
}
